//
//  RMineViewController.m
//  iAccountBook
//
//  Created by 侯伟 on 2021/8/27.
//

#import "RMineViewController.h"
#import "RSVerButton.h"
#import "RSSingleLineCell.h"

#import "RPlayHistoryViewController.h"
#import "RCacheListViewController.h"
#import "RCollectListViewController.h"
#import "RSBaseWebViewController.h"
#import "RFeedbackViewController.h"

#import "RCacheManager.h"
#import "RFeedbackRequest.h"

@interface RMineViewController ()

@property(nonatomic,strong) MyLinearLayout *contentLayout;

///用户协议
@property (nonatomic,weak)RSSingleLineCell* userProtocolCell;

///隐私协议
@property (nonatomic,weak)RSSingleLineCell* privateProtocolCell;

///清楚缓存
@property (nonatomic,weak)RSSingleLineCell* clearCacheCell;

///意见反馈
@property (nonatomic,weak)RSSingleLineCell* feedbackCell;

///求片
@property (nonatomic,weak)RSSingleLineCell* requestVideoCell;

@end

@implementation RMineViewController

- (void)loadView{
    [super loadView];
    
    MyLinearLayout *linearLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    linearLayout.backgroundColor = COLOR_BACKGEOUND;
    linearLayout.myMargin = 0;
    
    [self.view addSubview:linearLayout];
    
    //底部滚动控件
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
    }
    scrollView.myMargin = 0;
    scrollView.weight = 1.0;
    [linearLayout addSubview:scrollView];
    
    MyLinearLayout *contentLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    contentLayout.myHorzMargin = 0;
    contentLayout.myTop = 0;
    contentLayout.subviewVSpace = 10;
    contentLayout.heightSize.lBound(scrollView.heightSize, 10, 1);
    [scrollView addSubview:contentLayout];
    _contentLayout = contentLayout;

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self reloadCacheSize];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initSubViews];
    // Do any additional setup after loading the view.
}

- (void)initSubViews{
    
    MyLinearLayout *topLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    topLayout.backgroundColor = COLOR_WHITE;
    topLayout.myMargin = 0;
    topLayout.padding = UIEdgeInsetsMake(64, 0, 20, 0);
    topLayout.myHeight = MyLayoutSize.wrap;
    [self.contentLayout addSubview:topLayout];
    
    UIImageView* avatarView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mine_avatar"]];
    avatarView.myCenterX = 0;
    avatarView.myTop = 0;
    [avatarView sizeToFit];
    [topLayout addSubview:avatarView];
    
    MyLinearLayout *founctionLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Horz)];
    founctionLayout.myHorzMargin = 0;
    founctionLayout.myTop = 30;
    founctionLayout.gravity = MyGravity_Horz_Center;
    founctionLayout.subviewHSpace = 70;
    founctionLayout.myHeight = MyLayoutSize.wrap;
    [topLayout addSubview:founctionLayout];
    
    RSVerButton* history = [self addFunctionBtn:@"播放历史" imageName:@"mine_history" sel:@selector(historyClicked:)];
    [founctionLayout addSubview:history];
    
    RSVerButton* cache = [self addFunctionBtn:@"我的缓存" imageName:@"mine_cache" sel:@selector(cacheClicked:)];
    [founctionLayout addSubview:cache];
    
    RSVerButton* collect = [self addFunctionBtn:@"我的收藏" imageName:@"mine_collect" sel:@selector(collectClicked:)];
    [founctionLayout addSubview:collect];

    
    //用户协议
    WeakSelf
    RSSingleLineCell* userProtocolCell = [RSSingleLineCell cellWithTitle:@"用户协议"];
    userProtocolCell.myHorzMargin = 0;
    userProtocolCell.showSeparator = NO;
    userProtocolCell.clickBlock = ^{
        [weakSelf userProtocolClicked];
    };
    [self.contentLayout addSubview:userProtocolCell];
    _userProtocolCell = userProtocolCell;;
    
    //隐私协议
    RSSingleLineCell* privateProtocolCell = [RSSingleLineCell cellWithTitle:@"隐私协议"];
    privateProtocolCell.myHorzMargin = 0;
    privateProtocolCell.showSeparator = NO;
    privateProtocolCell.clickBlock = ^{
        [weakSelf privateProtocolClicked];
    };
    [self.contentLayout addSubview:privateProtocolCell];
    _privateProtocolCell = privateProtocolCell;
    
    
    //清理缓存
    RSSingleLineCell* clearCacheCell = [RSSingleLineCell cellWithTitle:@"清理缓存"];
    clearCacheCell.myHorzMargin = 0;
    clearCacheCell.showSeparator = NO;
    clearCacheCell.clickBlock = ^{
        [weakSelf clearCacheClicked];
    };
    [self.contentLayout addSubview:clearCacheCell];
    _clearCacheCell = clearCacheCell;
    
    //意见反馈
    RSSingleLineCell* feedbackCell = [RSSingleLineCell cellWithTitle:@"意见反馈"];
    feedbackCell.myHorzMargin = 0;
    feedbackCell.showSeparator = NO;
    feedbackCell.clickBlock = ^{
        [weakSelf feedbackClicked];
    };
    [self.contentLayout addSubview:feedbackCell];
    _feedbackCell = feedbackCell;
    
    //求片
    RSSingleLineCell* requestVideoCell = [RSSingleLineCell cellWithTitle:@"求片功能"];
    requestVideoCell.myHorzMargin = 0;
    requestVideoCell.showSeparator = NO;
    requestVideoCell.clickBlock = ^{
        [weakSelf requstVideos];
    };
    [self.contentLayout addSubview:requestVideoCell];
    _requestVideoCell = requestVideoCell;
}

- (RSVerButton*)addFunctionBtn:(NSString*)title imageName:(NSString*)imageName sel:(SEL)sel{
    RSVerButton* btn = [RSVerButton buttonWithType:(UIButtonTypeCustom)];
    btn.gap = 8;
    [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:COLOR_BLACK_DARK forState:UIControlStateNormal];
    btn.titleLabel.font = FONT_FIT(14);
    [btn addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
    btn.myMargin = 0;
    btn.mySize = CGSizeMake(PX(60), PX(60));
    return btn;
}

#pragma mark  -- 获取缓存大小

- (void)reloadCacheSize{
    self.clearCacheCell.subtitle = [RCacheManager.share cacheSizeStr];
}

#pragma mark  -- actions

- (void)historyClicked:(UIButton*)sender{
    [UIViewController showViewController:[RPlayHistoryViewController new] animated:YES];
}

- (void)cacheClicked:(UIButton*)sender{
    [UIViewController showViewController:[RCacheListViewController new] animated:YES];
}

- (void)collectClicked:(UIButton*)sender{
    [UIViewController showViewController:[RCollectListViewController new] animated:YES];
}

///用户协议
- (void)userProtocolClicked{
    RSBaseWebViewController* web = [RSBaseWebViewController new];
    web.cutsomerTitle = YES;
    web.title = @"用户协议";
    web.URL = [NSURL URLWithString:APP_PROTOCOL_URL];
    [UIViewController showViewController:web animated:YES];
}

///隐私协议
- (void)privateProtocolClicked{
    RSBaseWebViewController* web = [RSBaseWebViewController new];
    web.cutsomerTitle = YES;
    web.title = @"隐私协议";
    web.URL = [NSURL URLWithString:APP_PRIVATE_URL];
    [UIViewController showViewController:web animated:YES];
}

///清理缓存
- (void)clearCacheClicked{
    
    [RCacheManager.share removeAllCache];

    [self reloadCacheSize];
}

///意见反馈
- (void)feedbackClicked{
    RFeedbackViewController *feedback = [RFeedbackViewController new];
    [UIViewController showViewController:feedback animated:YES];
}


///求片
- (void)requstVideos{
    [Alert prompt:@"您想看什么影片呢?快来反馈给我们，我们将尽快为您安排!" text:NULL placeholder:@"请输入您想看的影片..." completion:^(NSString * _Nullable string) {
        if(string.length){
            
            RFeedbackRequest* request = [RFeedbackRequest new];
            request.advice = string;
            request.action = @"n";
            [RequestOperation encryptedRequest:request quite:YES callBack:NULL];
            
            [Toast toast:@"反馈成功,次日入库即可观看"];
        }
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

